package doh.client;

import doh.service.*;
import java.util.Properties;
import javax.swing.*;
import javax.naming.*;

public class DoHClientMain extends JFrame {

    /** Creates new form DoHClientMain */
    private InitialContext ctx;
    private UserSessionBeanRemote sessionBean;
    // change depending on who's ip
    private String ejbHost;

    // ATTN: use for Ravi, David, Rupal
    private final String JNDISessionBeanLookup = "java:global/DoH/DoH-ejb/UserSessionBean";
    // ATTN: use for Lucas (I couldn't get the entire app to deploy on my desktop)
//    private final String JNDISessionBeanLookup = "java:global/DoH-ejb/UserSessionBean";

    private DoHClientMain(String args[]) {
        super("Dance of Hands");
        ejbHost = args[0];
        initComponents();
        try {
            // for connection to localhost
//            ctx = new InitialContext();

            // for connection to remote glassfish container
            Properties props = new Properties();
            props.setProperty("java.naming.factory.initial",
                    "com.sun.enterprise.naming.SerialInitContextFactory");

            props.setProperty("java.naming.factory.url.pkgs",
                    "com.sun.enterprise.naming");

            props.setProperty("java.naming.factory.state",
                    "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");

            // sets ejb host to connect to, set with string above
            props.setProperty("org.omg.CORBA.ORBInitialHost", ejbHost);
            props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
            ctx = new InitialContext(props);
            sessionBean = (UserSessionBeanRemote) ctx.lookup("java:global/DoH/DoH-ejb/UserSessionBean");
        } catch (javax.naming.NamingException e){
            e.printStackTrace();
        }
        setupShutdownSequence(sessionBean);
        // load login panel
        JPanel lp = new LoginPanel(this.sessionBean);
        this.setContentPane(lp);
    }

     /**
     * makes sure we destroy the bean when we exit
     * this helps the server keep track of who is
     * logged in
     * @param sessionBean
     */
    private void setupShutdownSequence(final UserSessionBeanRemote sessionBean){
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                sessionBean.destroy();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(800, 600));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 600, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Handles logging in for users -
     * on success it will set the username,
     * indicating that the user is logged in
     * @param evt
     */
    /**
     * Handles registration for users
     * @param evt
     */
    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // Load login Panel

    }//GEN-LAST:event_formWindowActivated


    void loadLoginPanel(){
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
//        if(args.length != 1){
//            System.err.println("Usage: DoHClientMain <ejb_host_ip>");
//            System.exit(1);
//        }
        final String[] fargs = args;
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DoHClientMain frame = new DoHClientMain(fargs);
                frame.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
